<?php
namespace app\admin\controller;

use \think\Controller;
use \think\Db;
use app\admin\model\AuthMember;
use app\admin\model\MemberMenus;
use app\admin\model\Menus;

class BaseController extends Controller
{
	
	//判断是否登陆
    public function isLogin() {
        if (empty(session('member_id'))) {
            $this->error('请先登录', 'login/index');
        }
        $auth_member = new AuthMember();

        $auth_members = $auth_member->where(['member_id' => session('member_id')])->find();
        if(!$auth_members) {
        	$this->error('暂无登录权限', 'login/index');
        }
        $member_menus = new MemberMenus();
        $member_menuss = $member_menus->where(['auth_name' => $auth_members['auth_name']])->find();
        $member_list = explode(',', $member_menuss['menus_id']);
        if(!$member_list) {
        	$this->error('权限不存在', 'login/index');
        }
        $url =  substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], '/'), strripos($_SERVER['REQUEST_URI'], '.'));

        $url = substr($url, 7);
        $menus = new Menus();
        $menus_id = $menus->where(['url' => $url])->field('id, parent_id')->select()->toArray();
        if($menus_id) {
        	if(!in_array($menus_id[0]['id'], $member_list)) {
        		if($menus_id[0]['parent_id'] == '0') {
        			$this->error('暂无使用权限', 'login/index');
        		} elseif ($menus_id[0]['parent_id'] != '0') {
        			$this->error('暂无使用权限', $_SERVER["HTTP_REFERER"]);
        		}
	        }
        }
    }

}